package huangjinkuanggong;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class GameWin extends JFrame {
    //0未开始 1运行中 2 商店 3失败 4胜利
    static int state;

    //存储金块,石块
    ArrayList<Object> objectList = new ArrayList<>();
    BackGround backGround = new BackGround();
    Line line = new Line(this);
    {
        //是否可以放置
        boolean isPlace = true;
        for (int i = 0; i < 11; i++) {
            double random = Math.random();
            Gold gold;//存放生成的金块
            if (random<0.3){
                gold= new GoldMini();
            }else if(random<0.7){
                gold= new Gold();
            }else{
                gold= new GoldPlus();
            }
            for (Object object:objectList){
                if(gold.getRec().intersects(object.getRec())){
                    //不可放置。需要重新生成
                    isPlace =false;
                }
            }
            if(isPlace){
                objectList.add(gold);
            }else {
                isPlace = true;
                i--;
            }
        }
        for (int i = 0; i < 5; i++) {
            Rock rock = new Rock();
            for (Object object:objectList){
                if(rock.getRec().intersects(object.getRec())){
                    //不可放置。需要重新生成
                    isPlace =false;
                }
            }
            if(isPlace){
                objectList.add(rock);
            }else {
                isPlace=true;
                i--;
            }
        }
    }

    Image offScreenImage;

    void launch() {
        this.setVisible(true);
        this.setSize(800,800);
        this.setLocationRelativeTo(null);
        this.setTitle("ya民的黄金矿工");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton() == 1){
                    line.state = 1;
                }
            }
        });
        while (true){
            repaint();
            nextLevel();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    //下一关
    public void nextLevel(){
        if(BackGround.count > backGround.goal){
            BackGround.level++;
            dispose();
            GameWin gameWin = new GameWin();
            gameWin.launch();
        }
    }

    @Override
    public void paint(Graphics g) {
        offScreenImage = this.createImage(800,1000);
        Graphics gImage = offScreenImage.getGraphics();

        backGround.paintSelf(gImage);
        //绘制物体
        for (Object object:objectList){
            object.paintSelf(gImage);
        }
        line.paintSelf(gImage);//绘制金块
        g.drawImage(offScreenImage,0,0,null);
    }

    public static void main(String[] args) {
        GameWin gameWin = new GameWin();
        gameWin.launch();
    }
}

